<?php

/*
Plugin Name: LinkedIn Member List
Plugin URI: 
Description: Displays members linked in information. Developed by Ian Alexander for Arizona State University DISC. To use insert [linkedin-list] into any page or post.
Version: 1.20
Author: Ian Alexander
Author URI: 
*/

/****************************************************************
 * CHANGELOG
 *
 * 1.00 - Initial Release
 * 1.10 - Bug fixes regarding scheduling
 * 1.11 - Bug fix for LinkedIn Profiles with no Thumbnail
 * 1.12 - Fix to run update() on activation. Fixed formatting error.
 * 1.20 - Added feature to alphabetically sort list by last name.
 *        Also removed deprecated function get_users_of_blog().
 *
 ****************************************************************/
  

include_once('simple_html_dom.php');

/****************************************************************
 * Begin Code for Extra Profile Fields
 ***************************************************************/

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
 
function extra_user_profile_fields( $user ) { ?>
	<h3><?php _e("Linked In", "blank"); ?></h3>
 
	<table class="form-table">
	<tr>
	<th><label for="linkedin"><?php _e("LinkedIn Public URL"); ?></label></th>
	<td>
	<input type="text" name="linkedin" id="linkedin" value="<?php echo esc_attr( get_the_author_meta( 'linkedin_url', $user->ID ) ); ?>" class="regular-text" /><br />
	<span class="description"><?php _e("Please enter your linkedin public URL."); ?></span>
	</td>
	</tr>
	</table>
<?php }
 
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
 
function save_extra_user_profile_fields( $user_id ) { 
	if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
	update_usermeta( $user_id, 'linkedin_url', $_POST['linkedin'] );
}

/****************************************************************
 * Begin Code for Updating Member List
 ***************************************************************/
function update_linkedin_list()
{	
	// Get information about this blog's users into an array.
	$blogusers = get_users();
	$newContent = "";
	
	// Iterate over the array and retrieve that users' linkedin URL
	foreach ($blogusers as $usr) {
		// Try to get the user's linkedin profile
		$url = get_the_author_meta( 'linkedin_url', $usr->ID );
		$html = @file_get_html($url);
		if( $url != null && $html != null)
		{
			// Parse for key information we want
			$thumbnailSrc = $html->find('img.photo', 0)->src;
			$title = trim($html->find('p.title', 0)->innertext);
			$name = trim($html->find('span.given-name', 0)->innertext) . ' ' . trim($html->find('span.family-name', 0)->innertext);
			
			// Update user's metadata
			update_usermeta( $usr->ID, 'linkedin_thumb', 	$thumbnailSrc );
			update_usermeta( $usr->ID, 'linkedin_title', 	$title );
			update_usermeta( $usr->ID, 'linkedin_name', 	$name );
		}
		
		// Cleanup to avoid memory leaks
		$html->clear();
	    unset($html);
	}
	
	// Return the whole thing
	return '<div class="Linkedin-list">' . $newContent . '</div>';
}

/****************************************************************
 * Begin Code for shortcodes
 ***************************************************************/

add_shortcode('linkedin-list', 'linkedin_list_shortcode');

function linkedin_list_shortcode( $atts, $content = null ) {
	//update_linkedin_list(); //Enable this for debugging
	return display_linkedin_list();
}

function display_linkedin_list()
{	
	// Get information about this blog's users into an array.
	global $wpdb;
	$postids=$wpdb->get_col($wpdb->prepare("
		SELECT		users.ID
		FROM		$wpdb->users users, $wpdb->usermeta usermeta
		WHERE		users.ID = usermeta.user_id AND meta_key = 'last_name'	
		ORDER BY	usermeta.meta_value;"));
	$newContent = "";
	
	// Iterate over the array and retrieve that users' linkedin URL and other info
	foreach ($postids as $id) {
		$url = 		get_the_author_meta( 'linkedin_url', $id );
		$thumb =	get_the_author_meta( 'linkedin_thumb', $id );
		$title =	get_the_author_meta( 'linkedin_title', $id );
		$name =		get_the_author_meta( 'linkedin_name', $id );
		
		// Make sure we have information for all of these
		if( $url != null &&
			$title != null &&
			$name != null)
		{
			if($thumb == null)
			{
				$thumb = 'http://www.asudisc.org/wp-content/uploads/2011/01/BlankProfile.png';
			}
			$fname = split(' ', $name);
			
			// Create <div> for that user
			$newContent .= '<div class="linkedin-user" id="' . $usr->ID . '">' .
			'<img class="linkedin-thumb" src="' . $thumb . '"/>' . 
			'<span class="linkedin-name">' . $name . '</span></br> ' . 
			'<span class="linkedin-title">' . $title . '</span></br> ' . 
			'<a class="linkedin-view" href="' . $url . '">View ' .  
			$fname[0] . '</a></div>';	
		}
	}
	
	// Return the whole thing
	return '<div class="linkedin-list">' . $newContent . '</div>';
}
/****************************************************************
 * Begin Code for Conditionally adding CSS
 ***************************************************************/
add_action('wp_print_styles', 'linkedin_add_css'); // the_posts gets triggered before wp_head

function linkedin_add_css($posts){
	$myStyleUrl = WP_PLUGIN_URL . '/linkedin-list/linkedin-list.css';
	wp_register_style('linkedin-list-style', $myStyleUrl);
	wp_enqueue_style('linkedin-list-style');
}

/****************************************************************
 * Begin Code for Cron process
 ***************************************************************/
// Activation
register_activation_hook(__FILE__, 'linkedin_activation');
function linkedin_activation() {
	// Schedule at 3:00am
	wp_schedule_event(strtotime("2009-01-01 03:00:00"), 'daily', 'linkedin_daily_update');
	// Perform an update
	update_linkedin_list();
}
add_action('linkedin_daily_update', 'update_linkedin_list');

// Deactivation
register_deactivation_hook(__FILE__, 'linkedin_deactivation');
function linkedin_deactivation() {
	wp_clear_scheduled_hook('linkedin_daily_update');
}

?>